﻿using System;
using System.Windows;
using System.Windows.Input;

namespace myWindowsMediaPlayer
{
    public class MediaElementModel : ICommand
    {
        public bool isPlaying { set; get; }

        public MediaElementModel(MediaElementViewModel mediaElementViewModel)
        {
            // TODO: Complete member initialization
            this.mediaElementViewModel = mediaElementViewModel;
            this.isPlaying = false;
        }

        public bool CanExecute(object parameter)
        {
            return mediaElementViewModel.canExecuteCommand;
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            string action = (string)parameter;
            switch (action.ToLower())
            {
                case "play":
                    if (isPlaying)
                        mediaElementViewModel.mediaView.Pause();
                    else
                        mediaElementViewModel.mediaView.Play();
                    isPlaying = isPlaying ? false : true;
                    if (mediaElementViewModel.Source.Contains(".wmv") || mediaElementViewModel.Source.Contains(".avi") || mediaElementViewModel.Source.Contains(".mkv") || mediaElementViewModel.Source.Contains(".mp4"))
                        mediaElementViewModel.mediaView.Visibility = Visibility.Visible;
                    else
                        mediaElementViewModel.mediaView.Visibility = Visibility.Hidden;
                    break;
                case "stop":
                    mediaElementViewModel.mediaView.Stop();
                    isPlaying = false;
                    mediaElementViewModel.mediaView.Visibility = Visibility.Hidden;
                    break;
                case "pause":
                    mediaElementViewModel.mediaView.Pause();
                    isPlaying = false;
                    break;
                case "vol":
                    mediaElementViewModel.mediaView.IsMuted = mediaElementViewModel.mediaView.IsMuted ? false : true;
                    break;
                case "prev":
                    if (mediaElementViewModel.Source != null)
                    {
                        for (int i = 0; i < mediaElementViewModel.library.Count; i++)
                        {
                            if (mediaElementViewModel.Source == mediaElementViewModel.library[i]._path)
                            {
                                mediaElementViewModel.Source = ((--i) == -1 ? mediaElementViewModel.library[mediaElementViewModel.library.Count - 1]._path :
                                                                                                             mediaElementViewModel.library[i]._path);
                                break;
                            }
                        }
                    }
                    break;
                case "next":
                    for (int i = 0; i < mediaElementViewModel.library.Count; i++)
                    {
                        if (mediaElementViewModel.Source == mediaElementViewModel.library[i]._path)
                        {
                            mediaElementViewModel.Source = ((++i) == mediaElementViewModel.library.Count ? mediaElementViewModel.library[0]._path :
                                                                                                         mediaElementViewModel.library[i]._path);
                            break;
                        }
                    }
                    break;
                default:
                    throw new NotSupportedException(String.Format("Unknown media action {0}", action));
            }
        }

        private MediaElementViewModel mediaElementViewModel;
    }
}
